//
//  UIViewFSLoadding.m
//  QiCheZaZhi4iPhone
//
//  Created by David on 12-6-21.
//  Copyright (c) 2012年 VeryApps. All rights reserved.
//

#import "UIViewFSLoadding.h"

#import <QuartzCore/QuartzCore.h>

static UIViewFSLoadding *_shareLodding;

@implementation UIViewFSLoadding



- (id)initWithFrame:(CGRect)frame {
    self  = [super initWithFrame:frame];
    if (self) {
        _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        _activityView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
        CGRect rc = _activityView.frame;
        rc.size.width += 40;
        rc.size.height += 40;
        _viewLoaddingBg = [[UIView alloc] initWithFrame:rc];
        _viewLoaddingBg.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
        _viewLoaddingBg.layer.cornerRadius = 8;
        _viewLoaddingBg.layer.borderWidth = 0.6;
        _viewLoaddingBg.layer.borderColor = [UIColor darkGrayColor].CGColor;
        [_viewLoaddingBg addSubview:_activityView];
        _viewLoaddingBg.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
        
        _activityView.center = CGPointMake(floorf(_viewLoaddingBg.bounds.size.width/2), floorf(_viewLoaddingBg.bounds.size.height/2));
        _viewLoaddingBg.center = CGPointMake(floorf(self.bounds.size.width/2), floorf(self.bounds.size.height/2));
        
        [self addSubview:_viewLoaddingBg];
    }
    return  self;
}

+ (UIViewFSLoadding *)shareLoadding {
    if (!_shareLodding) {
        _shareLodding = [[UIViewFSLoadding alloc] initWithFrame:[[[UIApplication sharedApplication] keyWindow] bounds]];
    }
    return _shareLodding;
}

- (void)startWithAnimated:(BOOL)animated {
    NSArray *arrWindows = [[UIApplication sharedApplication] windows];
    UIWindow *window = [arrWindows objectAtIndex:0];
    self.frame = window.bounds;
    [_activityView startAnimating];
    if (animated) {
        self.alpha = 0;
        [window addSubview:self];
        [window bringSubviewToFront:self];
        [UIView animateWithDuration:0.5
                         animations:^{
                             self.alpha = 1;
                         } 
                         completion:^(BOOL finished) {
                             
                         }];
    }
    else {
        [window addSubview:self];
    }
}

- (void)stopWithAnimated:(BOOL)animated {
    if (animated) {
        [UIView animateWithDuration:0.5
                         animations:^{
                             self.alpha = 0;
                         } 
                         completion:^(BOOL finished) {
                             [self removeFromSuperview];
                             [_activityView stopAnimating];
                             self.alpha = 1;
                         }];
    }
    else {
        [_activityView stopAnimating];
        [self removeFromSuperview];
    }
}

@end
